using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using System.Collections;

namespace Mastermind
{
    public enum Colors { Blue, White, Red, Pink, Orange, Yellow, Green, Silver }

    public class Code : IEnumerable
    {
        public Colors[] Collection{get;set;}

        public Code()
        {
            Collection = new Colors[4];
        }

        //public Colors First { get; set; } 
        //public Colors Second { get; set; }
        //public Colors Third { get; set; }
        //public Colors Fourth { get; set; }

        public bool IsIn(Colors color)
        {
            for (int i = 0; i < Collection.Length; i++)
            {
                if (Collection[i] == color)
                {
                    return true;
                }
            }
            return false;
        }

        public IEnumerator GetEnumerator()
        {
            return Collection.GetEnumerator();
        }

        public bool Equals(Code code)
        {
            for (int i = 0; i < Collection.Length; i++)
            {
                if (code.Collection[i] != this.Collection[i])
                {
                    return false;
                }
            }
            return true;
        }
    }

    public class Mastermind : Microsoft.Xna.Framework.Game
    {

        Texture2D blue, red, pink, silver, white, green, yellow, orange;

        Code code = new Code();

        Random r = new Random();

        GraphicsDeviceManager graphics;
        SpriteBatch spriteBatch;

        public Mastermind()
        {
            graphics = new GraphicsDeviceManager(this);
            graphics.IsFullScreen = true;
            graphics.PreferredBackBufferWidth = 800;
            graphics.PreferredBackBufferHeight = 600;
            Content.RootDirectory = "Content";
        }

        protected override void Initialize()
        {
            base.Initialize();
            GenerateCode();
        }

        protected override void LoadContent()
        {
            spriteBatch = new SpriteBatch(GraphicsDevice);

            blue = Content.Load<Texture2D>("blue");
            green = Content.Load<Texture2D>("green");
            orange = Content.Load<Texture2D>("orange");
            pink = Content.Load<Texture2D>("pink");
            red = Content.Load<Texture2D>("red");
            silver = Content.Load<Texture2D>("silver");
            white = Content.Load<Texture2D>("white");
            yellow = Content.Load<Texture2D>("yellow");
        }

        protected override void UnloadContent()
        {
        }

        protected override void Update(GameTime gameTime)
        {
            KeyboardState s = Keyboard.GetState();
            if (s.IsKeyDown(Keys.Escape))
            {
                this.Exit();
            }
            if (s.IsKeyDown(Keys.Enter))
            {
                GenerateCode();
            }

            base.Update(gameTime);
        }

        protected override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.Black);
            base.Draw(gameTime);
            DrawCode();
        }


        private void GenerateCode()
        {
            int i = 0;
            while(true)
            {
                switch (r.Next(0, 7))
                {
                    case 0: if (!code.IsIn((Colors)0)) { code.Collection[i] = (Colors)0; i++;} break;
                    case 1: if (!code.IsIn((Colors)1)) { code.Collection[i] = (Colors)1; i++; } break;
                    case 2: if (!code.IsIn((Colors)2)) { code.Collection[i] = (Colors)2; i++; } break;
                    case 3: if (!code.IsIn((Colors)3)) { code.Collection[i] = (Colors)3; i++; } break;
                    case 4: if (!code.IsIn((Colors)4)) { code.Collection[i] = (Colors)4; i++; } break;
                    case 5: if (!code.IsIn((Colors)5)) { code.Collection[i] = (Colors)5; i++; } break;
                    case 6: if (!code.IsIn((Colors)6)) { code.Collection[i] = (Colors)6; i++; } break;
                    case 7: if (!code.IsIn((Colors)7)) { code.Collection[i] = (Colors)7; i++; } break;
                }
                if (i == 4)
                {
                    break;
                }
            }
        }

        private void DrawCode()
        {
            spriteBatch.Begin();
            for (int i = 0; i < code.Collection.Length; i++)
            {
                switch (code.Collection[i])
                {
                    case Colors.Blue: spriteBatch.Draw(blue, new Rectangle(0, i * blue.Width, blue.Width, blue.Height), Color.White); break;
                    case Colors.Green: spriteBatch.Draw(green, new Rectangle(0, i * green.Width, green.Width, green.Height), Color.White); break;
                    case Colors.Orange: spriteBatch.Draw(orange, new Rectangle(0, i * orange.Width, orange.Width, orange.Height), Color.White); break;
                    case Colors.Pink: spriteBatch.Draw(pink, new Rectangle(0, i * pink.Width, pink.Width, pink.Height), Color.White); break;
                    case Colors.Red: spriteBatch.Draw(red, new Rectangle(0, i * red.Width, red.Width, red.Height), Color.White); break;
                    case Colors.Silver: spriteBatch.Draw(silver, new Rectangle(0, i * silver.Width, silver.Width, silver.Height), Color.White); break;
                    case Colors.White: spriteBatch.Draw(white, new Rectangle(0, i * white.Width, white.Width, white.Height), Color.White); break;
                    case Colors.Yellow: spriteBatch.Draw(yellow, new Rectangle(0, i * yellow.Width, yellow.Width, yellow.Height), Color.White); break;
                }
            }
            spriteBatch.End();
        }
    }
}
